﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utility;

namespace Web
{
    public partial class index : System.Web.UI.Page
    {
        
        private IStatusTextService iStatusTextService = Utility.Factory.StatusTextEF();
        private IRoleService iRoleService = Utility.Factory.RoleEF();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DropDownList1.DataSourceID = null;
               // IStatusTextService IStatusTextService = new IStatusTextService();
                DropDownList1.DataSource = iStatusTextService.GetAll();//IServiceStatusText.GetAll();
                DropDownList1.DataBind();

                DropDownList1.Items.Insert(0, new ListItem("select", "0"));
                databind();
            }
        }

        private void databind()
       
        { 
            string roleName = "",status = "0";
           // string strwhere = " 1=1 ";
            if (txtRolename.Text.Trim().Length > 0)
               roleName = txtRolename.Text.Trim();
             if (DropDownList1.SelectedValue != "0")
                status = DropDownList1.SelectedValue;

            
            rptlist.DataSourceID = null;
            rptlist.DataSource = iRoleService.GetRole(txtRolename.Text.Trim(), int.Parse(DropDownList1.SelectedValue));
            //rptlist.DataSource = iRoleService.GetBySql(strwhere);
            rptlist.DataBind();
            if (DropDownList1.SelectedValue=="1")
            {
                btninuse.Visible = false;
                btnnouse.Visible = true;
            }
            else if (DropDownList1.SelectedValue == "2")
            {
                btninuse.Visible = true;
                btnnouse.Visible = false;
            }
            else
            {
                btninuse.Visible = true;
                btnnouse.Visible = true;
            }
           
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            try
            {
                if (e.CommandName == "del")
                    iRoleService.Delete(id);//IServiceRole.Delete(id);======================================================================
                databind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Button b1 = (Button)e.Item.FindControl("btndel");

            if (b1 != null)
            {
                b1.Attributes.Add("onclick", "return confirm(\"Delete This Data?\");");
            }
        }

        //protected void btninuse_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        for (int i = 0; i < this.rptlist.Items.Count; i++)
        //        {
        //            CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;
        //            if (chb.Checked == true)
        //            {
        //                int id = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
        //                iRoleService.Update(id, 1);//in use
        //            }
        //        }
        //        databind();
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex.ToString());
        //    }
        //}

        protected void btninuse_Click(object sender, EventArgs e)
        {
            //using (ServiceReference.RoleServiceClient clientProxy = new ServiceReference.RoleServiceClient())
            //{


                try
                {
                    List<Role> rlist = new List<Role>();
                    Role role;
                    for (int i = 0; i < this.rptlist.Items.Count; i++)
                    {
                        CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;

                        if (chb.Checked == true)
                        {
                            int id = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
                            role = iRoleService.GetById(id);
                            role.StatusText.StatusId = (int)RoleStatus.InUse;
                            rlist.Add(role);

                        }
                    }
                    iRoleService.Update(rlist);
                    databind();
                }

                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        //}
        protected void btnnouse_Click(object sender, EventArgs e)
        {
            try
            {
                List<Role> rlist = new List<Role>();
                Role role;
                for (int i = 0; i < this.rptlist.Items.Count; i++)
                {
                    CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;
                    if (chb.Checked == true)
                    {
                        int id = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
                        role = iRoleService.GetById(id);
                        role.StatusText.StatusId = (int)RoleStatus.NoUse;
                        rlist.Add(role);
                        
                    }
                }
                iRoleService.Update(rlist);
                databind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            databind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}